package com.societyGames.faceExercises
{
import com.societyGames.action.ActionQueue;

import flash.utils.Dictionary;

public class FaceSimulation
{
  [TypeHint(type="com.societyGames.faceExercises.FaceData")]
  public var people:Array = [];
  [TypeHint(type="String")]
  public var targetNames:Array = [];
  public var description:String;

  private var _render:FaceRender;
  private var _nameToIsPointedAt:Dictionary = new Dictionary(); //name:String -> Boolean

  public function get render():FaceRender
  {
    return this._render;
  }

  public function initialize(actionQueue:ActionQueue):void
  {
    this._render = new FaceRender(this, actionQueue);
    this._render.initialize();
  }

  public function pointAtPerson(name:String):void
  {
    this._nameToIsPointedAt[name] = true;

    var showSad:Boolean = targetNames.indexOf(name) == -1;
    this._render.pointAt(name, showSad);
  }

  public function stopPointAtPerson(name:String):void
  {
    this._nameToIsPointedAt[name] = false;
    this._render.stopPointAt(name);
  }

  public function calculateIsReportCorrect():Boolean
  {
    //Are the mystery people all pointed to?
    for each (var targetName:String in targetNames)
    {
      if (!this._nameToIsPointedAt[targetName])
      {
        return false;
      }
    }

    for (var name:String in this._nameToIsPointedAt)
    {
      //Not the mystery person and we have true or both
      if (_nameToIsPointedAt[name] && targetNames.indexOf(name) == -1)
      {
        return false; //Non mystery people were pointed at.
      }
    }
    return true; //Mystery person wasn't true.
  }

  public function getFaceData(name:String):FaceData
  {
    for each (var faceData:FaceData in people)
    {
      if (faceData.name == name)
      {
        return faceData;
      }
    }
    return null;
  }
}
}